﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--------------------------------------------------------------------------->  
<!--                           INTRODUCTION                                

 The Code Project article submission template (HTML version)

Using this template will help us post your article sooner. To use, just 
follow the 3 easy steps below:
 
     1. Fill in the article description details
     2. Add links to your images and downloads
     3. Include the main article text

That's all there is to it! All formatting will be done by our submission
scripts and style sheets. 

-->  
<!--------------------------------------------------------------------------->  
<!--                        IGNORE THIS SECTION                            -->
<html>
<head>
<title>Introduction to Green-forest Framework</title>
<Style>
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</style>
<link rel="stylesheet" type="text/css" href="http://www.codeproject.com/App_Themes/Std/CodeProject.css">
</head>
<body bgcolor="#FFFFFF" color=#000000>
<!--------------------------------------------------------------------------->  


<!-------------------------------     STEP 1      --------------------------->
<!--  Fill in the details (CodeProject will reformat this section for you) -->

<pre>
Title:       Introduction to Green-forest Framework 
Author:      evgeny.dolganov
Email:       evgenij.dolganov@gmail.com
Language:    Java
Platform:    Windows, Unix, Mac
Technology:  JEE, Spring
Level:       Intermediate
Description: This is the introduction to Green-forest Framework with creating a web application example
Section      Languages
SubSection   Java
License:     CPOL
</pre>

<!-------------------------------     STEP 2      --------------------------->
<!--  Include download and sample image information.                       --> 

<ul class=download>
<li><a href="http://green-forest.googlecode.com/files/web-app-example.war">Download demo project - 2.2 Mb </a></li>
<li><a href="http://green-forest.googlecode.com/files/web-app-example-src.zip">Download source - 28 Kb</a></li>
</ul>

<p><img src="http://green-forest.googlecode.com/svn/trunk/reference-guide/i/gf-logo-label.png" alt="Green-forest Logo" width=240 height=164></p>


<!-------------------------------     STEP 3      --------------------------->

<!--  Add the article text. Please use simple formatting (<h2>, <p> etc)   -->
<h2>Introduction</h2>
<a target="_blank" href='http://code.google.com/p/green-forest'>Green-forest Framework</a> is the simple IoC Container with Action-Handler architecture. 
It's not a competitor for Spring Framework or JEE but it's a powerful addition for these frameworks. 
You can use Green-forest for a single class implementation and for a complex business logic.

<p>
The Action-Handler architecture can be presents by this scheme:
<br>
<img src="http://green-forest.googlecode.com/svn/tags/0.9/reference-guide/i/action-handler-pattern.png"/>
<br>
As you see on the scheme we shared our logic into some classes:
<ol>
<li>
<h4>Action</h4>
Action class presents an "atomic business method" and contains input and output data:
<pre lang="java">
//Action with String input and Integer output data
public class SomeAction extends Action&lt;String, Integer&gt;{
 
    public SomeAction(String input) {
        super(input);
    }
     
}</pre>
</li>
<li>
<h4>Handler</h4>
Handler class presents an implementation of business logic:
<pre lang="java">
@Mapping(SomeAction.class)
public class SomeHandler extends Handler&lt;SomeAction&gt;{
 
    @Inject
    SomeService service;
     
    public void invoke(SomeAction action) throws Exception {
     
        String input = action.getInput();
        Integer result = service.doSomeWork(input);
         
        action.setOutput(result);
    }
}</pre>
	
</li>
<li>
<h4>Framework</h4>
Framework's Engine ensures functioning of handlers:
<pre lang="java">
//create Engine instance
Engine engine = new Engine();
 
//register the handler
engine.putHandler(SomeHandler.class);
 
//invoke the action
Integer result = engine.invoke(new SomeAction("some data"));
</pre>
</li>
</ol>


<h2>Motivation</h2>
What is the motivation for using Green-forest Framework? 
The main task is to simplify the code: transfer of one big class (some service implementation) to 
a set of small classes (handlers).
<p>See the example:
<br>"Classic" implementation presents one big class on 1000 lines of code:
<pre lang="java">
//"classic" impl of API
public class SomeBigServiceImpl implements SomeService {
	
	public Doc createDoc(...){...}
	
	public Doc createDocWithAttach(...){...}
	
	public Doc getDocById(...){...}
	
	public List getDocsList(...){...}
	
	//and many many other methods on 1000 lines of a code
	
}</pre>

<br>Action-Handler implementation presents a set of small classes:
<pre lang="java">
//Action-Handler API
public class CreateDocHandler extends Handler&lt;CreateDoc&gt;{
  //10-20 lines of code
}

public class CreateDocWithAttachHandler extends Handler&lt;CreateDocWithAttach&gt;{
  //10-20 lines of code
}

public class GetDocByIdHandler extends Handler&lt;GetDoc&gt;{
  //10-20 lines of code
}

public class GetDocListHandler extends Handler&lt;GetDocList&gt;{
  //10-20 lines of code
}</pre>


<h2>Example Application</h2>
<h3>Overview</h3>
Let's review a simple example application with Green-forest Framework.
It presents the web interface for users and uses a database for storage.
By this application we can create, read, update and delete simple objects.
<p>View of the application's web interface:
<br><img src="http://green-forest.googlecode.com/svn/trunk/extra/external/gf-example-app.png"/>
<p>You can download binaries and sources of this example from the article's download section.
After downloading "web-app-example.war" file, deploy it into your Web Server. This application was tested on the Tomcat 7.0.28 web server.

<h3>Explanation of Example</h3>
First we present the model class for CRUD operations - Doc:
<pre lang="java">
package example.common.model;

public class Doc {
	public long id;
	public String name;
}
</pre>

<p>
After that we create the set of actions - our business API:
<pre>
src/example/common/action/
	CreateDataBase.java
	CreateDocs.java
	GetDocsCount.java
	GetDocsPage.java
	RenameDoc.java
</pre>

<p>Every class of this set is the action class:
<pre lang="java">
package example.common.action;

public class RenameDoc extends Action&lt;Doc, Void&gt; {
	
	public RenameDoc(int id, String newName){
		this(new Doc(id, newName));
	}

}</pre>

<p>So we can use this actions in our web layer:
<pre lang="java">
package example.web;

public class AppServlet extends HttpServlet {

	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		
		//get data for update
		int id = Util.tryParse(req.getParameter("id"), -1);
		String newName = req.getParameter("name");
		
		//invoke update action
		application.invoke(new RenameDoc(id, newName));
	}

}</pre>

<p>And for every action we create its own handler:
<pre lang="java">
package jdbc.storage.handler;

@Mapping(RenameDoc.class)
public class RenameDocHandler extends Handler&lt;RenameDoc&gt;{
	
	@Inject
	Connection c;

	@Override
	public void invoke(RenameDoc action) throws Exception {
		
		Doc input = action.input();
		
		PreparedStatement ps = c.prepareStatement("UPDATE doc SET name=? WHERE id=?");
		ps.setInt(2, input.id);
		ps.setString(1, input.name);
		ps.executeUpdate();
		
	}

}</pre>

<p>That's it! We received a set of compact simple classes for API (Actions) and for implementation (Handlers).

<h2>Summary</h2>
<a target="_blank" href='http://code.google.com/p/green-forest'>Green-forest Framework</a> presents 
a new perspective on the development of Java applications. It's a free open-source tool
which can simplify a code of your application. This library requires minimum time to learn and is suitable 
for a wide range of tasks. Sо happy coding with it!




<!-------------------------------    That's it!   --------------------------->
</body>

</html>

